package homework.qingming.calc.ui;

import homework.qingming.calc.Calculator;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import java.security.Key;
import java.text.ParseException;
import java.util.Arrays;
import java.util.EmptyStackException;

public class CalcUI {
    private JPanel panel1;
    private JTextArea textArea1;
    private JTextField resultTextField;
    private JButton a3Button;
    private JButton a1Button;
    private JButton a2Button;
    private JButton a5Button;
    private JButton a4Button;
    private JButton a6Button;
    private JButton a7Button;
    private JButton a8Button;
    private JButton a9Button;
    private JButton a0Button;
    private JButton doButton;
    private JButton a0Button1;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton backButton;
    private JButton lpButton;
    private JButton rpButton;
    private JButton sqrtButton;
    private JButton absButton;
    private JButton button7;
    private JButton PIButton;
    private JButton eButton;
    private JButton sumButton;

    private Calculator calc;

    public CalcUI() {
        calc = Calculator.createCalc();
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String t = ((JButton) actionEvent.getSource()).getText();
                textArea1.insert(t, textArea1.getCaretPosition());
            }
        };
        a1Button.addActionListener(listener);
        a2Button.addActionListener(listener);
        a5Button.addActionListener(listener);
        a4Button.addActionListener(listener);
        a6Button.addActionListener(listener);
        a3Button.addActionListener(listener);
        a7Button.addActionListener(listener);
        a8Button.addActionListener(listener);
        a9Button.addActionListener(listener);
        a0Button.addActionListener(listener);
        a0Button1.addActionListener(listener);
        button1.addActionListener(listener);
        button2.addActionListener(listener);
        button3.addActionListener(listener);
        button4.addActionListener(listener);
        button5.addActionListener(listener);
        button7.addActionListener(listener);
        lpButton.addActionListener(listener);
        rpButton.addActionListener(listener);
        PIButton.addActionListener(listener);
        eButton.addActionListener(listener);

        // 清空
//        button6.addActionListener((e) -> textArea1.setText(""));
        button6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                textArea1.setText("");
                resultTextField.setText("0");
            }
        });
        // 退格
//        backButton.addActionListener(e -> {
//            String txt = textArea1.getText();
//            if(!txt.isEmpty()) {
//                int cp = textArea1.getCaretPosition();
//                textArea1.replaceRange("", cp - 1, cp);
//            }
//        });
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String txt = textArea1.getText();
                if (!txt.isEmpty()) {
                    int cp = textArea1.getCaretPosition();
                    textArea1.replaceRange("", cp - 1, cp);
                }
            }
        });
        doButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    if (!textArea1.getText().isEmpty()) {
                        BigDecimal result = calc.calculation(textArea1.getText());
                        resultTextField.setText(result.toString());
                    }
                } catch (EmptyStackException e) {
                    resultTextField.setText("Error: 表达式错误");
                } catch (ParseException | NumberFormatException | ArithmeticException e) {
                    resultTextField.setText("Error: " + e.getMessage());
                }
            }
        });

//        ActionListener funcListener = actionEvent -> {
//            String t = ((JButton)actionEvent.getSource()).getText() + "(";
//            textArea1.insert(t, textArea1.getCaretPosition());
//        };
        ActionListener funcListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String t = ((JButton) actionEvent.getSource()).getText() + "(";
                textArea1.insert(t, textArea1.getCaretPosition());
            }
        };
        sqrtButton.addActionListener(funcListener);
        sumButton.addActionListener(funcListener);
        absButton.addActionListener(funcListener);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("计算器");
        frame.setContentPane(new CalcUI().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * @noinspection ALL
     */
    private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
        if (currentFont == null) return null;
        String resultName;
        if (fontName == null) {
            resultName = currentFont.getName();
        } else {
            Font testFont = new Font(fontName, Font.PLAIN, 10);
            if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
                resultName = fontName;
            } else {
                resultName = currentFont.getName();
            }
        }
        return new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        panel1 = new JPanel();
        panel1.setLayout(new BorderLayout(0, 0));
        panel1.setFocusable(false);
        panel1.setMaximumSize(new Dimension(29, 36));
        panel1.setPreferredSize(new Dimension(400, 250));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        panel2.setFocusable(false);
        panel2.setMaximumSize(new Dimension(29, 32767));
        panel2.setPreferredSize(new Dimension(400, 100));
        panel1.add(panel2, BorderLayout.NORTH);
        textArea1 = new JTextArea();
        textArea1.setColumns(30);
        textArea1.setFocusCycleRoot(false);
        textArea1.setFocusTraversalPolicyProvider(false);
        Font textArea1Font = this.$$$getFont$$$(null, -1, 14, textArea1.getFont());
        if (textArea1Font != null) textArea1.setFont(textArea1Font);
        textArea1.setRows(3);
        textArea1.setText("");
        panel2.add(textArea1);
        resultTextField = new JTextField();
        resultTextField.setColumns(34);
        resultTextField.setEditable(false);
        resultTextField.setFocusable(true);
        Font resultTextFieldFont = this.$$$getFont$$$(null, -1, 14, resultTextField.getFont());
        if (resultTextFieldFont != null) resultTextField.setFont(resultTextFieldFont);
        resultTextField.setText("0");
        panel2.add(resultTextField);
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new GridBagLayout());
        panel3.setFocusable(false);
        panel1.add(panel3, BorderLayout.CENTER);
        a1Button = new JButton();
        a1Button.setFocusable(false);
        a1Button.setText("1");
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(a1Button, gbc);
        a2Button = new JButton();
        a2Button.setFocusable(false);
        a2Button.setText("2");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(a2Button, gbc);
        a5Button = new JButton();
        a5Button.setFocusable(false);
        a5Button.setText("5");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(a5Button, gbc);
        a4Button = new JButton();
        a4Button.setFocusable(false);
        a4Button.setText("4");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(a4Button, gbc);
        a6Button = new JButton();
        a6Button.setFocusable(false);
        a6Button.setText("6");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(a6Button, gbc);
        a3Button = new JButton();
        a3Button.setFocusable(false);
        a3Button.setText("3");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(a3Button, gbc);
        a7Button = new JButton();
        a7Button.setFocusable(false);
        a7Button.setText("7");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(a7Button, gbc);
        a8Button = new JButton();
        a8Button.setFocusable(false);
        a8Button.setText("8");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(a8Button, gbc);
        a9Button = new JButton();
        a9Button.setFocusable(false);
        a9Button.setText("9");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(a9Button, gbc);
        a0Button = new JButton();
        a0Button.setFocusable(false);
        a0Button.setText("0");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(a0Button, gbc);
        a0Button1 = new JButton();
        a0Button1.setFocusable(false);
        a0Button1.setText(".");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(a0Button1, gbc);
        final JPanel spacer1 = new JPanel();
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(spacer1, gbc);
        button1 = new JButton();
        button1.setFocusable(false);
        button1.setText("+");
        gbc = new GridBagConstraints();
        gbc.gridx = 4;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(button1, gbc);
        button2 = new JButton();
        button2.setFocusable(false);
        button2.setText("-");
        gbc = new GridBagConstraints();
        gbc.gridx = 4;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(button2, gbc);
        button3 = new JButton();
        button3.setFocusable(false);
        button3.setText("*");
        gbc = new GridBagConstraints();
        gbc.gridx = 4;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(button3, gbc);
        button4 = new JButton();
        button4.setFocusable(false);
        button4.setText("/");
        gbc = new GridBagConstraints();
        gbc.gridx = 4;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(button4, gbc);
        button5 = new JButton();
        button5.setFocusable(false);
        button5.setText("%");
        gbc = new GridBagConstraints();
        gbc.gridx = 5;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(button5, gbc);
        final JPanel spacer2 = new JPanel();
        gbc = new GridBagConstraints();
        gbc.gridx = 6;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(spacer2, gbc);
        lpButton = new JButton();
        lpButton.setFocusable(false);
        lpButton.setText("(");
        gbc = new GridBagConstraints();
        gbc.gridx = 5;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(lpButton, gbc);
        rpButton = new JButton();
        rpButton.setFocusable(false);
        rpButton.setText(")");
        gbc = new GridBagConstraints();
        gbc.gridx = 5;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(rpButton, gbc);
        doButton = new JButton();
        doButton.setFocusable(false);
        doButton.setText("=");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(doButton, gbc);
        button7 = new JButton();
        button7.setFocusable(false);
        button7.setText(",");
        gbc = new GridBagConstraints();
        gbc.gridx = 5;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(button7, gbc);
        PIButton = new JButton();
        PIButton.setFocusable(false);
        PIButton.setText("PI");
        gbc = new GridBagConstraints();
        gbc.gridx = 7;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(PIButton, gbc);
        eButton = new JButton();
        eButton.setFocusable(false);
        eButton.setText("e");
        gbc = new GridBagConstraints();
        gbc.gridx = 7;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(eButton, gbc);
        button6 = new JButton();
        button6.setFocusable(false);
        button6.setText("清空");
        gbc = new GridBagConstraints();
        gbc.gridx = 7;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(button6, gbc);
        backButton = new JButton();
        backButton.setFocusable(false);
        backButton.setText("退格");
        gbc = new GridBagConstraints();
        gbc.gridx = 7;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel3.add(backButton, gbc);
        final JPanel panel4 = new JPanel();
        panel4.setLayout(new GridBagLayout());
        panel4.setFocusable(false);
        panel1.add(panel4, BorderLayout.EAST);
        final JPanel spacer3 = new JPanel();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel4.add(spacer3, gbc);
        final JPanel spacer4 = new JPanel();
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.fill = GridBagConstraints.VERTICAL;
        panel4.add(spacer4, gbc);
        absButton = new JButton();
        absButton.setFocusable(false);
        absButton.setText("abs");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel4.add(absButton, gbc);
        final JLabel label1 = new JLabel();
        label1.setText("函数");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        panel4.add(label1, gbc);
        sumButton = new JButton();
        sumButton.setFocusable(false);
        sumButton.setText("sum");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel4.add(sumButton, gbc);
        sqrtButton = new JButton();
        sqrtButton.setFocusable(false);
        sqrtButton.setText("sqrt");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel4.add(sqrtButton, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return panel1;
    }
}
